package com.fair.concurrency.juc3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;

public class Demo1CyclicBarrier
{
    public static void main(String[] args)
    {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

        List<Thread> threadList = new ArrayList<>();
        for (int i = 1; i < 6; i++)
        {
            Thread t = new Thread(new Athlete(cyclicBarrier, "运动员" + i));
            threadList.add(t);
        }

        for (Thread t : threadList)
        {
            t.start();
        }
    }

    static class Athlete implements Runnable
    {
        private CyclicBarrier cyclicBarrier;
        private String name;

        public Athlete(CyclicBarrier cyclicBarrier, String name)
        {
            this.cyclicBarrier = cyclicBarrier;
            this.name = name;
        }

        @Override
        public void run()
        {
            System.out.println(name + "就位");
            try
            {
                cyclicBarrier.await();
                System.out.println(name + "到达终点。");
            } catch (Exception e)
            {
            }
        }
    }
}